/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author QUY_THI
 */
@ManagedBean
@SessionScoped
public class LoginBean {

    @EJB
    private AdminLoginLocal adminLogin;
    private String Username;
    private String Password;

    public String getPassword() {
        return Password;
    }

    public void setPassword(String Password) {
        this.Password = Password;
    }

    public String getUsername() {
        return Username;
    }

    public void setUsername(String Username) {
        this.Username = Username;
    }

    /** Creates a new instance of LoginBean */
    public LoginBean() {
    }

    public String checkLogin() {
        boolean result = adminLogin.adminLogin(Username, Password);
        if (result) {
            return "home.xhtml?faces-redirect=true";
        }
        FacesMessage msg = new FacesMessage("Error", "Username or Password invalid !!");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        return "index";
    }

    public String getAutoRedirect() throws IOException {
        if (Username == null || Username.equals("")) {
            FacesContext context = FacesContext.getCurrentInstance();
            ((HttpServletResponse) context.getExternalContext().getResponse()).sendRedirect("../faces/index.xhtml");

        }
        return "";
    }
}
